#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);        
    // 初始化子窗口，必须在绑定信号槽前完成
    this->create=new AddInfo();
    // 初始化及加载数据
    this->initTableWidget();
    this->setSignals();    
    // QObject::connect(create, SIGNAL(triggered()), MainWindow, SLOT(addData()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initTableWidget() {
//    qDebug()<<this->COL_NUM;
    ui->tableWidget->setColumnCount(this->COL_NUM);

    QStringList header;
    header.append(QObject::tr("编号"));
    header.append(QObject::tr("菜品名称"));
    header.append(QObject::tr("价格"));
    header.append(QObject::tr("图片"));
    header.append(QObject::tr("商家id"));
    header.append(QObject::tr("备注"));
    ui->tableWidget->setHorizontalHeaderLabels(header);//标题头部
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);//行末不留空
    //ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
    // ui->tableWidget->setSelectionBehavior ( QAbstractItemView::SelectRows); //设置选择行为，以行为单位
    // ui->tableWidget->setSelectionMode ( QAbstractItemView::SingleSelection); //设置选择模式，选择单行

    this->reload();
}

// 设置型号槽连接
void MainWindow::setSignals(){
    QObject::connect(ui->create,SIGNAL(triggered()),this,SLOT(addData()));
    QObject::connect(ui->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(editData(int,int)));
    QObject::connect(ui->del,SIGNAL(triggered()),this,SLOT(deleteData()));
    QObject::connect(ui->reload,SIGNAL(triggered()),this,SLOT(reload()));
    QObject::connect(this->create,SIGNAL(sendMsg(int)),this,SLOT(receiveMsg(int)));//子父窗口信号槽绑定
}

void MainWindow::reload(){
    MySQLDBHelper dbHelper = MySQLDBHelper();
    QString sql = "select * from dishes";
    QSqlQuery query = dbHelper.getQuery();
    query.exec(sql);

    ui->tableWidget->setRowCount(query.size());

    int i = 0;
    while (query.next()) {
        for (int j = 0; j < this->COL_NUM; j++) {
            ui->tableWidget->setItem(i, j, new QTableWidgetItem(query.value(j).toString()));
        }
        i++;
    }
}

void MainWindow::addData() {
    // AddInfo* create=new AddInfo(this);
    // this->create=new AddInfo(0);
    this->create->setId(0);
    this->create->show();
}

// 双击动作默认会传入两个int值，一个是行号（从0开始），一个是列号（从0开始）
void MainWindow::editData(int row,int col) {
//    int row=ui->tableWidget->currentIndex().row();
    QTableWidgetItem* item=new QTableWidgetItem;
    //该行的第一列的内容，编号，就是这条记录的id值
    item=ui->tableWidget->item(row,0);
//    qDebug()<<item->text();
    int id=item->text().toInt();
    this->create->setId(id);
    this->create->show();
}

void MainWindow::deleteData(){
    QList<QTableWidgetItem*> selectedItems=ui->tableWidget->selectedItems();
    if(selectedItems.length()==0){
        QMessageBox::about(NULL, "警告", "未选择相关信息");
        return;
    }
    QTableWidgetItem* idItem=selectedItems.at(0);
    QString id=idItem->text();
    // qDebug()<<id;
    QMessageBox msgBox(QMessageBox::NoIcon, "删除确定", "确定删除该信息？", QMessageBox::Yes|QMessageBox::No,NULL);
    if(msgBox.exec()==QMessageBox::No){
        return;
    }
    MySQLDBHelper dbHelper=MySQLDBHelper();
    QSqlQuery query=dbHelper.getQuery();
    QString sql="delete from dishes where id='"+id+"'";
    query.exec(sql);
    if(query.numRowsAffected()==0){
        qDebug()<<query.lastError()<<sql;
    }
    this->reload();
    // int count=items.count();
//     for(int i=0;i<count;i++){
//         QTableWidgetItem* item=items.at(i);
//         QString name=item->text();
//         qDebug()<<name;
//     }
    
}

void MainWindow::receiveMsg(int stat) {
    // qDebug()<<stat;
    if(stat==0){
        this->reload();
    }
}
